package com.manger.chrome;

import com.google.common.collect.ImmutableMap;
import com.manger.utils.DataTools;
import com.manger.utils.DriverUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.events.EventFiringDecorator;
import org.openqa.selenium.support.events.WebDriverListener;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.swing.*;
import java.io.File;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

public class DriverManger {

    public static ChromeDriver driver;

    public static void init() throws Exception {
        init(null, null);
    }

    public static void init(String ip, Integer port) throws Exception {
        if (driver != null) {
            try {
                driver.quit();
            } catch (Exception ex) {

            }
        }
        String driverPath = System.getProperty("user.dir") + "/../chromedriver-win64/chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", driverPath);
        System.out.println(driverPath);
        File file = new File(driverPath);
        System.out.println(file.getCanonicalPath());
        String tempDirPath = System.getProperty("user.dir") + "/" + UUID.randomUUID().toString();
        ChromeOptions options = new ChromeOptions();
        //options.addExtensions(new File("C:\\Users\\liuhj\\Desktop\\proxy.zip"));
        options.addArguments("--port=14446");
        if (ip != null && port != null) {
            Proxy proxy = new Proxy();
            proxy.setSslProxy(ip + ":" + port);
            proxy.setHttpProxy(ip + ":" + port);
            options.setProxy(proxy);
        }
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("unexpectedAlertBehaviour", "accept");
        capabilities.setCapability("pageLoadStrategy", "normal");
        capabilities.setCapability("timeouts", ImmutableMap.of("implicit", 10, "pageLoad", 30, "script", 30));

        options.addArguments("--no-sandbox");
        options.addArguments("--disable-dev-shm-usage");
        options.addArguments("--user-data-dir=" + tempDirPath);
        options.addArguments("--disable-blink-features=AutomationControlled"); // 防止被识别为自动化工具的控制浏览器行为（可选）
        options.addArguments("--disable-dev-shm-usage");
        options.addArguments("--remote-debugging-port=19222");
        options.addArguments("--remote-allow-origins=*");
        // 禁止显示"Chrome 正受到自动测试软件的控制。"
        options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
        driver = new ChromeDriver(options);

        driver.get("https://zxfw.court.gov.cn/zxfw/#/pagesGrxx/pc/login/index");




        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                if(DriverManger.driver != null){
                    System.out.println("=========DriverManager=========");
                    DriverManger.driver.quit();
                }
            }
        });
    }

    public static boolean hasOnline() {
        if (DriverManger.driver == null) {
            return false;
        }
        try {
            String title = driver.getTitle();
            return true;
        } catch (Exception ee) {
        }
        return false;
    }

    public static void main(String[] args) {
        //DriverManger.init("114.80.142.38", 58360);
        // DriverManger.init("125.94.215.64", 16817,"iepelqan","td5vu7pq");
    }
}